package com.dctp.ems.config;

import com.dctp.common.core.RedisManager;
import com.dctp.common.disruptor.close.CloseOrderDisruptor;
import com.dctp.common.intercept.CorsInterceptor;
import com.dctp.ems.controller.chart.ChartQueryManager;
import com.dctp.ems.interceptor.AuthInterceptor;
import com.dctp.ems.utils.OpsLogUtil;
import com.jfinal.config.*;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;

public class EmsConfig extends JFinalConfig {

    private static final Logger logger = LoggerFactory.getLogger(EmsConfig.class);

    @Override
    public void configConstant(Constants constants) {
        constants.setBaseUploadPath("/data/upload/images/admin");
        constants.setDevMode(true);
    }

    @Override
    public void configRoute(Routes routes) {
        routes.scan("com.dctp.ems.controller");
    }

    @Override
    public void configEngine(Engine engine) {

    }

    @Override
    public void configPlugin(Plugins plugins) {

        Prop p = PropKit.use("project.properties");
        DruidPlugin dp = new DruidPlugin(p.get("url"), p.get("username"), p.get("password"), p.get("driverClassName"));
        dp.setMinIdle(4);
        dp.setInitialSize(1);
        dp.setMaxWait(2000L);
        dp.setMaxActive(8);
        plugins.add(dp);
        ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
        arp.setTransactionLevel(Connection.TRANSACTION_READ_COMMITTED);
        plugins.add(arp);
        logger.info("数据库连接成功...");

        // redis
        String redisHost = p.get("redis.host");
        int redisPort = p.getInt("redis.port");

        RedisManager.init(redisHost, redisPort);
        logger.info("redis连接成功...");
    }

    @Override
    public void configInterceptor(Interceptors interceptors) {
        interceptors.add(new CorsInterceptor());
        interceptors.add(new AuthInterceptor());
    }

    @Override
    public void configHandler(Handlers handlers) {

    }

    @Override
    public void onStart() {
        //OpsLogUtil.init();
        ChartQueryManager.init();
    }

    public static void main(String[] args) {
        UndertowServer.create(EmsConfig.class, "undertow.properties")
                .start();

    }
}
